<?php
 
defined("_VALID_ACCESS") || die('Direct access forbidden');
 
class SCTV_AcceptanceCommon extends ModuleCommon {
 
    public static function menu() {
    	$menu2 = array(
    			'__submenu__' => 2,        		
		);
		
		if(Base_AclCommon::check_permission('Report:Acceptance:Fee Material Deposit Subnet'))
			$menu2[__('Acceptance Report')] = array('__function__'=>'acceptance_report');

        $manager =  CRM_ContactsCommon::get_my_record();
        $is_area_manager = array_key_exists('area manager', $manager['access']);
        if($is_area_manager)
            $menu2[__('Acceptance Change Request')] = array('__function__'=>'acceptance_change_request');
		
    	return array(__('SCTV') => array(
    		'__submenu__' => 1, __('Warehouse') => $menu2 
		));
    }
	public static function display_money($record, $nolink, $desc) {
		$money = $record[$desc['id']];
		return  Utils_CurrencyFieldCommon::format($money);
	}
	public static function display_checked($record, $nolink, $desc) {
		$checked = $record[$desc['id']];
		if($checked)
        {
            if(Base_AclCommon::check_permission('Acceptance:Check'))
            {
                $href =  Base_BoxCommon::main_module_instance()->create_confirm_callback_href('Are you sure?', array('SCTV_AcceptanceCommon', 'update_unchecked'), array('acceptance'=>$record['id']));
                return '<a ' . $href . '>' . __('Checked') . '</a>';
            }
            else
                return __('Checked');
        }
		else
		{
			if(Base_AclCommon::check_permission('Acceptance:Check'))
			{				
				$href =  Base_BoxCommon::main_module_instance()->create_confirm_callback_href('Are you sure?', array('SCTV_AcceptanceCommon', 'update_checked'), array('acceptance'=>$record['id']));
				return '<a ' . $href . '>' . __('Unchecked') . '</a>';
			}
			else
				return __('Unchecked');
		}			
	}
	public static function QFfield_checked(&$form, $field, $label, $mode, $default, $desc, $rb_obj){
		if($default)
        {
            if(Base_AclCommon::check_permission('Acceptance:Check'))
            {
                $href =  Base_BoxCommon::main_module_instance()->create_confirm_callback_href(_M('Are you sure?'), array('SCTV_AcceptanceCommon', 'update_unchecked'), array('acceptance'=>$rb_obj->record['id']));
                $form->addElement('static', $field, $label, '<a ' . $href . '>' . __('Checked') . '</a>');
            }
            else
                $form->addElement('static', $field, $label, __('Checked'));
        }
		else
		{
			if(Base_AclCommon::check_permission('Acceptance:Check'))
			{
				$href =  Base_BoxCommon::main_module_instance()->create_confirm_callback_href(_M('Are you sure?'), array('SCTV_AcceptanceCommon', 'update_checked'), array('acceptance'=>$rb_obj->record['id']));
				$form->addElement('static', $field, $label, '<a ' . $href . '>' . __('Unchecked') . '</a>');	
			}					
			else {
				$form->addElement('static', $field, $label, __('Unchecked'));
			}
		}	
	}
	public static function update_checked($acceptance)
	{
		Utils_RecordBrowserCommon::update_record('SCTV_Acceptance', $acceptance, array('checked'=>true));
	}
    public static function update_unchecked($acceptance)
    {
        Utils_RecordBrowserCommon::update_record('SCTV_Acceptance', $acceptance, array('checked'=>false));
    }
	public static function QFfield_nhan_vien_quyet_toan(&$form, $field, $label, $mode, $default, $desc, $rb_obj){
		if($mode=='view')
		{
			$contact = CRM_ContactsCommon::get_contact_by_user_id($rb_obj->record['created_by']);
			$form->addElement('static', $field, $label, CRM_ContactsCommon::contact_format_no_company($contact, false));	
		}			
	}
	public static function display_material_type($record, $nolink, $desc) {
		$material_type = $record[$desc['id']];
		switch ($material_type) {
			case 0:
				return 'Có thu phí';							
				break;
			case 1:
				return 'Miễn phí';
				break;				
			case 2:
				return 'Cho mạng phụ';
				break;
			default:
				return 'Sai rồi cu';
				break;
		}
	}
    public static function display_acceptance_request_status($record, $nolink, $desc) {
        $status = $record[$desc['id']];

        $select_status = Utils_RecordBrowser::$rb_obj->init_module('Utils_LeightboxPrompt');
        $form = Utils_RecordBrowser::$rb_obj->init_module('Libs_QuickForm');
        $form->addElement('text', 'note', __('Note'));
        $form->addRule('note', 'Field required', 'Required');

        $result = null;
        switch($status){
            case 0:
                $select_status->add_option('Fixed', __('Fixed'), null, $form);

                // Neu co quyen dong
                if(Base_AclCommon::check_permission('Acceptance:Check'))
                    $select_status->add_option('Close', __('Close'), null);

                Utils_RecordBrowser::$rb_obj->display_module($select_status, array(__( 'Change status'), array('acceptancechangerequest')));
                $href = $select_status->get_href(array('acceptancechangerequest' => $record['id']));
                $result = '<a ' . $href . '>Đang yêu cầu</a>';
                break;
            case 1:
                if(Base_AclCommon::check_permission('Acceptance:Check')){
                    $select_status->add_option('Reopen', __('Reopen'), null, $form);
                    $select_status->add_option('Close', __('Close'), null);
                    Utils_RecordBrowser::$rb_obj->display_module($select_status, array(__( 'Change status'), array('acceptancechangerequest')));
                    $href = $select_status->get_href(array('acceptancechangerequest' => $record['id']));
                    $result = '<a ' . $href . '>Đã sửa</a>';
                }
                else
                    $result = 'Đã sửa';
                break;
            case 2:
                $result = 'Đóng yêu cầu';
                break;
        }

        $vals = $select_status->export_values();
        if (isset($vals['params']['acceptancechangerequest']) && $vals['params']['acceptancechangerequest'] == $record['id'] && !isset($_REQUEST['__location']))
        {
            if ($vals['option'] == 'Reopen'){
                Utils_RecordBrowserCommon::update_record('SCTV_AcceptanceChangeRequest', $record['id'], array('status' =>0, 'note'=>$vals['form']['note']));
                location(array());
            }
            elseif ($vals['option'] == 'Fixed'){
                Utils_RecordBrowserCommon::update_record('SCTV_AcceptanceChangeRequest', $record['id'], array('status' =>1, 'result'=>$vals['form']['note']));
                location(array());
            }
            elseif ($vals['option'] == 'Close'){
                Utils_RecordBrowserCommon::update_record('SCTV_AcceptanceChangeRequest', $record['id'], array('status' =>2));
                location(array());
            }
        }

        return $result;
    }
}
 
?>